package cn.com.jrr.core.service.power;



import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.dao.impl.BaseDaoImpl;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.PageModel;
import cn.com.jrr.core.model.power.SysRole;
import cn.com.jrr.core.model.power.SysUserRole;

@Service
public class SysRoleService {
	@Resource
	private BaseDaoImpl<SysRole> baseDaoImpl;
	@Autowired
	PageModel <SysRole> pageModel;
	public DataTableResponse<SysRole> list(DataTableRequest dataTableRequest){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="SELECT * FROM sysrole";
		Object [] obj=new String[]{"roleId","roleCode","roleName","roleDescription"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<SysRole> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,SysRole.class);
		return  dataTableResponse;
	}
	/**
	 * 添加数据
	 * */
	public void add(SysRole sysRole){
		baseDaoImpl.save(sysRole);
	}
	/**
	 * 获取一条
	 * */
	public SysRole get(Integer id){
		return  baseDaoImpl.get(SysRole.class, id);
	}
	/**
	 * 删除记录
	 * */
	public int delete(Integer id){
		return  baseDaoImpl.updateBySql("delete from sysrole where roleId=?", new Object[]{id});
	}
	/**
	 * 更新记录
	 * */
	public void update(SysRole sysRole){
		baseDaoImpl.update(sysRole);;
	}
	public void saveUserRole(SysUserRole sysUserRole){
		baseDaoImpl.save(sysUserRole);
	}
	public DataTableResponse<SysRole> getRoleByUserId(DataTableRequest dataTableRequest,Integer userId){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="SELECT * FROM sysrole WHERE roleId IN(SELECT roleId FROM sysuserrole WHERE userId=?)";
		Object [] obj=new String[]{"roleId","roleCode","roleName","roleDescription"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<SysRole> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,SysRole.class,new Object[]{userId});
		return  dataTableResponse;
	}


}
